<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class NewsController extends Controller
{
   //新闻列表
	public function getIndex(Request $request)
	{
		// dd($request->all());
		// $bid = $request->input('bid');
		if ($request->input('keywords')) {
			
        	$news = DB::table('news')->select(DB::raw('*,count(comments.cid) as num'))->leftJoin('comments', 'news.nid', '=', 'comments.cnid')->where('news.nname','like','%'.$request->input('keywords').'%')->orWhere('news.ntitle','like','%'.$request->input('keywords').'%')->groupBy('news.nid')->orderBy('news.nid','desc')->paginate(10);
			
		}else{

        	$news = DB::table('news')->select(DB::raw('*,count(comments.cid) as num'))->leftJoin('comments', 'news.nid', '=', 'comments.cnid')->groupBy('news.nid')->orderBy('news.nid','desc')->paginate(10);
		}
        //查询新闻列表
        // $news = DB::table('news')->select(DB::raw('news.*,comments.*,count(comments.cid) as num'))->leftJoin('comments','news.nid','=','comments.cnid')->paginate(10);
       
        // dd($news->items());
        //按热度查询新闻
        $hot = DB::table('news')->select(DB::raw('news.nid,news.ntitle'))->orderBy('rnum','desc')->skip(0)->take(10)->get();
        // dd($hot);
        //获取list
        $list = $request->all();
		// //解析模版
		return view('Home.new.index',['news'=>$news,'hot'=>$hot,'list'=>$list]);
	}

	//新闻详情
	public function getDetial(Request $request)
	{
		//获取nid
		$nid = $request->input('nid');
		//查询新闻详情
        $news = DB::table('news')->select(DB::raw('*,count(comments.cid) as num'))->leftJoin('users', 'news.nuid', '=', 'users.uid')->leftJoin('comments', 'news.nid', '=', 'comments.cnid')->where('nid','=',$nid)->first();
        //查询评论详情
        $comments = DB::table('comments')->select(DB::raw('comments.*,users.username,users.uid,users.pic'))->leftJoin('users','comments.cuid','=','users.uid')->where('cnid','=',$nid)->get();
        //获取评论数
        $num = DB::select('select COUNT(*) as num from `comments` left join `users` on `comments`.`cuid` = `users`.`uid` where cnid = ?',[$nid])[0];
        // dd($n);
        //按热度查询新闻
        $hot = DB::table('news')->select(DB::raw('news.nid,news.ntitle'))->orderBy('rnum','desc')->skip(0)->take(10)->get();
        
        return view('Home.new.detial',['news'=>$news,'comments'=>$comments,'nid'=>$nid,'num'=>$num,'hot'=>$hot]);
	}

	//评论添加
	public function getComments(Request $request)
	{
		$data = $request->only('cnid','cncont');
		$data['cntime'] = date('Y-m-d H:i:s',time());
		$data['cuid'] = session('uid');
		// $data['cuid'] = 6;
		$res = DB::table('comments')->insertGetId($data);
		if ($res) {
			//添加成功
			echo 1;
		}else{
			//添加失败
			echo 0;
		}
	}

	//点赞处理
	public function getClike(Request $request)
	{
		//获取cid
		$cid = $request->input('cid');
		//数据库操作
		$res = DB::update('update comments set clike = clike + 1 where cid = ?', [$cid]);
		if ($res) {
			//操作成功
			echo 1;
		}else{
			//操作失败
			echo 0;
		}
	} 

	//阅览量添加
	public function getAddyue(Request $request)
	{
		$nid = $request->input('nid');
		//贴子阅览量加1
		$res = DB::update('update news set rnum = rnum + 1 where nid = ?', [$nid]);
		if ($res) {
			//修改成功
			echo 1;
		}else{
			//修改失败
			echo 0;
		}
	}
}
